package com.maotai.llzhou.util;

import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import com.maotai.llzhou.bean.HuaRunInfo;

public class HuaRunHttpClientUtil {
	public static String Host = "app.crv.com.cn";
	public static String Connection = "keep-alive";
	public static String language = "zh-CN";
	public static String UserAgent = "Mozilla/5.0 (Linux; Android 6.0.1; OPPO A57 Build/MMB29M; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/46.0.2490.76 Mobile Safari/537.36";
	public static String osVersion = "6.0.1";
	public static String Accept = "application/json, text/plain, */*";
	
	public static String networkType = "wifi";
	public static String channel = "mobile";
	public static String os = "android";
	public static String appkey = "ef1fc57c13007e33";
	public static String appVersion = "2.3.0";
	public static String AcceptEncoding = "gzip, deflate";
	public static String AcceptLanguage = "zh-CN,en-US;q=0.8";
	public static String XRequestedWith = "com.crv.wanjia";
	
	//post����
	public static String ContentType = "application/x-www-form-urlencoded;charset=UTF-8";
	public static String Origin = "file://";
	
	
	private static HuaRunHttpClientUtil instance = null;
	
	private HuaRunHttpClientUtil() {
	}

	public static HuaRunHttpClientUtil getInstance() {
		if (instance == null) {
			instance = new HuaRunHttpClientUtil();
		}
		return instance;
	}
	
	public HuaRunInfo sendHttpGetProxy(String httpUrl, HuaRunInfo huaRunInfo, RequestConfig config) throws Exception {
		HttpGet httpGet = new HttpGet(httpUrl);
		httpGet = setHttpGetHeader(httpGet,huaRunInfo);
		return doHttpGetProxy(httpGet,huaRunInfo,config);
	}
	private HttpGet setHttpGetHeader(HttpGet httpGet, HuaRunInfo huaRunInfo) {
		httpGet.addHeader("Host",Host);
		httpGet.addHeader("Connection", Connection);
		httpGet.addHeader("language", language);
		httpGet.addHeader("User-Agent", UserAgent);
		httpGet.addHeader("osVersion", osVersion);
		httpGet.addHeader("Accept", Accept);
		httpGet.addHeader("networkType",networkType);
		
		httpGet.addHeader("channel",channel);
		httpGet.addHeader("os",os);
		httpGet.addHeader("appkey",appkey);
		httpGet.addHeader("appVersion",appVersion);
		httpGet.addHeader("Accept-Encoding",AcceptEncoding);
		httpGet.addHeader("Accept-Language",AcceptLanguage);
		httpGet.addHeader("X-Requested-With",XRequestedWith);
		
		
		//����������ֵ����ôЭ��ͷ�ͼ���
		if(huaRunInfo!=null && !"".equals(huaRunInfo.getUniqueId()) && huaRunInfo.getUniqueId()!=null)
			httpGet.addHeader("unique","android-"+huaRunInfo.getUniqueId());
		if(huaRunInfo!=null && !"".equals(huaRunInfo.getUserSession()) && huaRunInfo.getUserSession()!=null)
			httpGet.addHeader("userSession",huaRunInfo.getUserSession());
		if(huaRunInfo!=null && !"".equals(huaRunInfo.getSubsiteId()) && huaRunInfo.getSubsiteId()!=null)
			httpGet.addHeader("subsiteId",huaRunInfo.getSubsiteId());
		if(huaRunInfo!=null && !"".equals(huaRunInfo.getUserId()) && huaRunInfo.getUserId()!=null)
			httpGet.addHeader("userId",huaRunInfo.getUserId());
		if(huaRunInfo!=null && !"".equals(huaRunInfo.getJsessionId()) && huaRunInfo.getJsessionId()!=null)
			httpGet.addHeader("Cookie",huaRunInfo.getJsessionId());
		return httpGet;
	}
	private HuaRunInfo doHttpGetProxy(HttpGet httpGet,HuaRunInfo huaRunInfo, RequestConfig config) throws Exception {
		CloseableHttpClient httpClient = null;
		CloseableHttpResponse response = null;
		HttpEntity entity = null;
		String return_str = null;
		httpClient = HttpClients.createDefault();
		if(config!=null)
			httpGet.setConfig(config);
		response = httpClient.execute(httpGet);
		entity = response.getEntity();
		return_str = EntityUtils.toString(entity, "UTF-8");
		Header[] headers = response.getAllHeaders();
		if(headers!=null&&headers.length>0){
        	for (Header hea : headers) {
				if(hea.getName().contains("Set-Cookie")||hea.getName().contains("set-cookie")){
					String temp = hea.getValue();
					if(temp.contains("DISTRIBUTED_JSESSIONID")){
						huaRunInfo.setJsessionId(temp);
					}
				}
			}
        }
		huaRunInfo.setContent(return_str);
		System.out.println(return_str);
	
		return huaRunInfo;
	}
	public HuaRunInfo sendHttpPostProxy(String httpUrl, HuaRunInfo huaRunInfo, String data, RequestConfig config) throws Exception {
		HttpPost httpPost = new HttpPost(httpUrl);
		httpPost = setHttpPostHeader(httpPost,huaRunInfo);
		httpPost.setEntity(new StringEntity(data)); 
		return doHttpPostProxy(httpPost,huaRunInfo,config);
	}
	private HttpPost setHttpPostHeader(HttpPost httpPost, HuaRunInfo huaRunInfo) {
		httpPost.addHeader("Host",Host);
		httpPost.addHeader("Connection", Connection);
		httpPost.addHeader("language", language);
		httpPost.addHeader("User-Agent", UserAgent);
		httpPost.addHeader("osVersion", osVersion);
		httpPost.addHeader("Accept", Accept);
		httpPost.addHeader("networkType",networkType);
		
		httpPost.addHeader("channel",channel);
		httpPost.addHeader("os",os);
		httpPost.addHeader("appkey",appkey);
		httpPost.addHeader("appVersion",appVersion);
		httpPost.addHeader("Accept-Encoding",AcceptEncoding);
		httpPost.addHeader("Accept-Language",AcceptLanguage);
		httpPost.addHeader("X-Requested-With",XRequestedWith);
		
		//post����
		httpPost.addHeader("Content-Type",ContentType);
		httpPost.addHeader("Origin",Origin);
		
		//����������ֵ����ôЭ��ͷ�ͼ���
		if(huaRunInfo!=null && !"".equals(huaRunInfo.getUniqueId()) && huaRunInfo.getUniqueId()!=null)
			httpPost.addHeader("unique","android-"+huaRunInfo.getUniqueId());
		if(huaRunInfo!=null && !"".equals(huaRunInfo.getUserSession()) && huaRunInfo.getUserSession()!=null)
			httpPost.addHeader("userSession",huaRunInfo.getUserSession());
		if(huaRunInfo!=null && !"".equals(huaRunInfo.getSubsiteId()) && huaRunInfo.getSubsiteId()!=null)
			httpPost.addHeader("subsiteId",huaRunInfo.getSubsiteId());
		if(huaRunInfo!=null && !"".equals(huaRunInfo.getUserId()) && huaRunInfo.getUserId()!=null)
			httpPost.addHeader("userId",huaRunInfo.getUserId());
		if(huaRunInfo!=null && !"".equals(huaRunInfo.getJsessionId()) && huaRunInfo.getJsessionId()!=null)
			httpPost.addHeader("Cookie",huaRunInfo.getJsessionId());
		return httpPost;
	}
	private HuaRunInfo doHttpPostProxy(HttpPost httpPost,HuaRunInfo huaRunInfo,RequestConfig config) throws Exception{  
        HttpClient httpClient = null;  
        HttpResponse response = null;  
        HttpEntity entity = null;
		String return_str = null;
        httpClient = HttpClients.createDefault(); 
        if(config!=null)
        	httpPost.setConfig(config);  
        response = httpClient.execute(httpPost); 
        entity = response.getEntity();
		return_str = EntityUtils.toString(entity, "UTF-8");
		Header[] headers = response.getAllHeaders();
		if(headers!=null&&headers.length>0){
        	for (Header hea : headers) {
				if(hea.getName().contains("Set-Cookie")||hea.getName().contains("set-cookie")){
					String temp = hea.getValue();
					if(temp.contains("DISTRIBUTED_JSESSIONID")){
						huaRunInfo.setJsessionId(temp);
					}
				}
			}
        }
		huaRunInfo.setContent(return_str);
		System.out.println(return_str);
        return huaRunInfo;  
    } 
}